package com.bytes.activemq.queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class Queue_Producer {
	//JmsMessagingTemplate是Springboot的Jms模板,Spring的是JmsTemplate
	private final JmsMessagingTemplate jmsMessagingTemplate;

	private final ActiveMQQueue activeMQQueue;

	/**
	 * 发送一条消息到Queue
	 */
	public void producerMsg() {
		jmsMessagingTemplate.convertAndSend(activeMQQueue, "**************" + UUID.randomUUID().toString());
	}

	/**
	 * 间隔3秒投递,SpringBoot的Scheduled用来定时执行
	 */
	@Scheduled(fixedDelay = 3000)
	public void producerMsgScheduled() {
		String s = UUID.randomUUID().toString();
		jmsMessagingTemplate.convertAndSend(activeMQQueue, "**************Scheduled" + s);
		System.out.println("Scheduled定时投递:\t"+ s);
	}

	@Autowired
	public Queue_Producer(JmsMessagingTemplate jmsMessagingTemplate, ActiveMQQueue activeMQQueue) {
		this.jmsMessagingTemplate = jmsMessagingTemplate;
		this.activeMQQueue = activeMQQueue;
	}
}


